<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>菜单管理系统</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            min-height: 100vh;
            margin: 0;
            padding: 0;
        }

        .background-container {
            position: fixed;
            top: 0;
            left: 0;
            width: 100vw;
            height: 100vh;
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            z-index: -1;
            transition: all 0.3s ease;
        }

        .app-container {
            min-height: 100vh;
            background: rgba(255, 255, 255, 0.85);
            backdrop-filter: blur(3px);
            position: relative;
            z-index: 1;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px 0;
            text-align: center;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }

        .controls {
            background: rgba(255, 255, 255, 0.95);
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            align-items: center;
            backdrop-filter: blur(5px);
            border-bottom: 1px solid rgba(255, 255, 255, 0.3);
        }

        .search-container {
            flex: 1;
            min-width: 250px;
            position: relative;
        }

        .search-input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 25px;
            font-size: 16px;
            outline: none;
            transition: border-color 0.3s ease;
        }

        .search-input:focus {
            border-color: #667eea;
        }

        .btn {
            padding: 12px 20px;
            border: none;
            border-radius: 25px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 600;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
        }

        .btn-secondary {
            background: #6c757d;
            color: white;
        }

        .btn-secondary:hover {
            background: #5a6268;
            transform: translateY(-2px);
        }

        .main-content {
            padding: 20px;
            max-width: 1200px;
            margin: 0 auto;
        }

        .menu-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }

        .menu-card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 15px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            overflow: hidden;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            position: relative;
            backdrop-filter: blur(5px);
            border: 1px solid rgba(255, 255, 255, 0.3);
        }

        .menu-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(0,0,0,0.15);
        }

        .menu-image {
            width: 100%;
            height: 200px;
            object-fit: cover;
            background: linear-gradient(45deg, #f0f0f0 25%, transparent 25%),
                        linear-gradient(-45deg, #f0f0f0 25%, transparent 25%),
                        linear-gradient(45deg, transparent 75%, #f0f0f0 75%),
                        linear-gradient(-45deg, transparent 75%, #f0f0f0 75%);
            background-size: 20px 20px;
            background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
        }

        /*.menu-image {*/
        /*    width: 100%;*/
        /*    max-height: 200px; !* 使用最大高度而不是固定高度 *!*/
        /*    object-fit: contain; !* 完整显示图片 *!*/
        /*    background: none; !* 移除背景图案 *!*/
        /*    border-radius: 4px; !* 可选：添加圆角 *!*/
        /*    display: block; !* 确保正确显示 *!*/
        /*}*/

        .menu-content {
            padding: 20px;
        }

        .menu-title {
            font-size: 1.2rem;
            font-weight: bold;
            color: #333;
            margin-bottom: 8px;
        }

        .menu-price {
            font-size: 1.1rem;
            color: #e74c3c;
            font-weight: bold;
            margin-bottom: 10px;
        }

        .menu-description {
            color: #666;
            font-size: 14px;
            line-height: 1.4;
            margin-bottom: 15px;
        }

        .menu-actions {
            display: flex;
            gap: 10px;
        }

        .btn-small {
            padding: 8px 15px;
            font-size: 12px;
            border-radius: 15px;
        }

        .btn-edit {
            background: #28a745;
            color: white;
        }

        .btn-delete {
            background: #dc3545;
            color: white;
        }

        .btn-edit:hover, .btn-delete:hover {
            opacity: 0.8;
            transform: translateY(-1px);
        }

        .modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 1000;
            opacity: 0;
            visibility: hidden;
            transition: all 0.3s ease;
        }

        .modal.show {
            opacity: 1;
            visibility: visible;
        }

        .modal-content {
            background: rgba(255, 255, 255, 0.98);
            padding: 30px;
            border-radius: 15px;
            width: 90%;
            max-width: 500px;
            max-height: 90vh;
            overflow-y: auto;
            transform: scale(0.7);
            transition: transform 0.3s ease;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.5);
        }

        .modal.show .modal-content {
            transform: scale(1);
        }

        .modal-header {
            text-align: center;
            margin-bottom: 20px;
        }

        .modal-title {
            font-size: 1.5rem;
            color: #333;
            margin-bottom: 10px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #555;
        }

        .form-input {
            width: 100%;
            padding: 12px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 16px;
            outline: none;
            transition: border-color 0.3s ease;
        }

        .form-input:focus {
            border-color: #667eea;
        }

        .form-textarea {
            min-height: 80px;
            resize: vertical;
        }

        .image-preview {
            margin-top: 10px;
            text-align: center;
        }

        .preview-image {
            max-width: 100%;
            max-height: 200px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .modal-actions {
            display: flex;
            gap: 10px;
            justify-content: flex-end;
            margin-top: 20px;
        }

        .loading {
            text-align: center;
            padding: 40px;
            color: #666;
            font-size: 16px;
        }

        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #666;
        }

        .empty-state-icon {
            font-size: 4rem;
            margin-bottom: 20px;
            opacity: 0.5;
        }

        .load-more {
            text-align: center;
            margin: 30px 0;
        }

        .background-controls {
            background: rgba(255, 255, 255, 0.95);
            padding: 15px;
            border-radius: 10px;
            margin-bottom: 20px;
            backdrop-filter: blur(5px);
            border: 1px solid rgba(255, 255, 255, 0.3);
        }

        .background-title {
            font-size: 1.1rem;
            font-weight: bold;
            margin-bottom: 10px;
            color: #333;
        }

        @media (max-width: 768px) {
            .controls {
                flex-direction: column;
                align-items: stretch;
            }

            .search-container {
                min-width: 100%;
            }

            .menu-grid {
                grid-template-columns: 1fr;
            }

            .modal-content {
                margin: 20px;
                width: calc(100% - 40px);
            }
        }
    </style>
</head>
<body>
    <div id="app">
        <!-- 背景容器 -->
        <div class="background-container" :style="backgroundStyle"></div>

        <div class="app-container">
            <header class="header">
                <h1>🍽️ 菜单管理系统</h1>
                <p>轻松管理您的餐厅菜单</p>
            </header>

            <div class="controls">
                <div class="search-container">
                    <input
                        type="text"
                        class="search-input"
                        placeholder="🔍 搜索菜品名称..."
                        v-model="searchQuery"
                        @input="handleSearch"
                    >
                </div>
                <button class="btn btn-primary" @click="openAddModal">
                    ➕ 添加菜品
                </button>
                <button class="btn btn-secondary" @click="openBackgroundModal">
                    🖼️ 设置背景
                </button>
            </div>

            <div class="main-content">


                <div v-if="loading" class="loading">
                    <div>🔄 加载中...</div>
                </div>

                <div v-else-if="displayedMenus.length === 0" class="empty-state">
                    <div class="empty-state-icon">🍽️</div>
                    <h3>暂无菜品</h3>
                    <p>点击"添加菜品"按钮开始添加您的第一道菜品</p>
                </div>

                <div v-else>
                    <div class="menu-grid">
                        <div
                            v-for="menu in displayedMenus"
                            :key="menu.id"
                            class="menu-card"
                        >
                            <img
                                v-if="menu.imageUrl"
                                :src="menu.imageUrl"
                                :alt="menu.name"
                                class="menu-image"
                                @error="handleImageError"
                            >
                            <div v-else class="menu-image"></div>

                            <div class="menu-content">
                                <h3 class="menu-title">{{ menu.name }}</h3>
                                <div class="menu-price">¥{{ menu.price }}</div>
                                <p class="menu-description">{{ menu.description }}</p>

                                <div class="menu-actions">
                                    <button
                                        class="btn btn-edit btn-small"
                                        @click="openEditModal(menu)"
                                    >
                                        ✏️ 编辑
                                    </button>
                                    <button
                                        class="btn btn-delete btn-small"
                                        @click="deleteMenu(menu.id)"
                                    >
                                        🗑️ 删除
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div v-if="hasMore" class="load-more">
                        <button class="btn btn-primary" @click="loadMore">
                            📄 加载更多
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 添加/编辑菜品模态框 -->
        <div class="modal" :class="{ show: showMenuModal }" @click="closeMenuModal">
            <div class="modal-content" @click.stop>
                <div class="modal-header">
                    <h2 class="modal-title">
                        {{ isEditing ? '编辑菜品' : '添加菜品' }}
                    </h2>
                </div>

                <form @submit.prevent="saveMenu">
                    <div class="form-group">
                        <label class="form-label">菜品名称</label>
                        <input
                            type="text"
                            class="form-input"
                            v-model="currentMenu.name"
                            placeholder="请输入菜品名称"
                            required
                        >
                    </div>

                    <div class="form-group">
                        <label class="form-label">价格 (元)</label>
                        <input
                            type="number"
                            class="form-input"
                            v-model="currentMenu.price"
                            placeholder="请输入价格"
                            step="0.01"
                            min="0"
                            required
                        >
                    </div>

                    <div class="form-group">
                        <label class="form-label">菜品描述</label>
                        <textarea
                            class="form-input form-textarea"
                            v-model="currentMenu.description"
                            placeholder="请输入菜品描述"
                        ></textarea>
                    </div>

                    <div class="form-group">
                        <label class="form-label">菜品图片URL</label>
                        <input
                            type="url"
                            class="form-input"
                            v-model="currentMenu.imageUrl"
                            placeholder="请输入图片URL地址"
                            @input="previewImage"
                        >
                        <div v-if="currentMenu.imageUrl" class="image-preview">
                            <img
                                :src="currentMenu.imageUrl"
                                alt="预览图"
                                class="preview-image"
                                @error="handlePreviewError"
                            >
                        </div>
                    </div>

                    <div class="modal-actions">
                        <button type="button" class="btn btn-secondary" @click="closeMenuModal">
                            取消
                        </button>
                        <button type="submit" class="btn btn-primary">
                            {{ isEditing ? '更新' : '添加' }}
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- 背景设置模态框 -->
        <div class="modal" :class="{ show: showBackgroundModal }" @click="closeBackgroundModal">
            <div class="modal-content" @click.stop>
                <div class="modal-header">
                    <h2 class="modal-title">设置背景图片</h2>
                </div>

                <form @submit.prevent="saveBackground">
                    <div class="form-group">
                        <label class="form-label">背景图片URL</label>
                        <input
                            type="url"
                            class="form-input"
                            v-model="tempBackgroundUrl"
                            placeholder="请输入背景图片URL地址"
                        >
                        <div v-if="tempBackgroundUrl" class="image-preview">
                            <img
                                :src="tempBackgroundUrl"
                                alt="背景预览"
                                class="preview-image"
                                @error="handleBackgroundPreviewError"
                            >
                        </div>
                    </div>

                    <div class="modal-actions">
                        <button type="button" class="btn btn-secondary" @click="clearBackground">
                            清除背景
                        </button>
                        <button type="button" class="btn btn-secondary" @click="closeBackgroundModal">
                            取消
                        </button>
                        <button type="submit" class="btn btn-primary">
                            保存
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        const { createApp, ref, computed, onMounted, nextTick } = Vue;

        createApp({
            setup() {
                // 响应式数据
                const menus = ref([]);
                const searchQuery = ref('');
                const loading = ref(false);
                const showMenuModal = ref(false);
                const showBackgroundModal = ref(false);
                const isEditing = ref(false);
                const currentMenuId = ref(null);
                const displayCount = ref(6);
                const backgroundUrl = ref('');
                const tempBackgroundUrl = ref('');

                // 当前菜品表单数据
                const currentMenu = ref({
                    name: '',
                    price: '',
                    description: '',
                    imageUrl: ''
                });

                // 计算属性
                const backgroundStyle = computed(() => {
                    if (backgroundUrl.value) {
                        console.log('设置背景图片:', backgroundUrl.value);
                        return {
                            backgroundImage: `url("${backgroundUrl.value}")`,
                            display: 'block'
                        };
                    }
                    return {
                        display: 'none'
                    };
                });

                const filteredMenus = computed(() => {
                    if (!searchQuery.value.trim()) {
                        return menus.value;
                    }
                    return menus.value.filter(menu =>
                        menu.name.toLowerCase().includes(searchQuery.value.toLowerCase())
                    );
                });

                const displayedMenus = computed(() => {
                    return filteredMenus.value.slice(0, displayCount.value);
                });

                const hasMore = computed(() => {
                    return displayedMenus.value.length < filteredMenus.value.length;
                });

                // 方法
                const loadMenus = () => {
                    const stored = localStorage.getItem('restaurant-menus');
                    if (stored) {
                        try {
                            menus.value = JSON.parse(stored);
                        } catch (e) {
                            console.error('加载菜单数据失败:', e);
                            menus.value = [];
                        }
                    }
                };

                const saveMenus = () => {
                    localStorage.setItem('restaurant-menus', JSON.stringify(menus.value));
                };

                const loadBackground = () => {
                    const stored = localStorage.getItem('restaurant-background');
                    if (stored) {
                        backgroundUrl.value = stored;
                    }
                };

                const saveBackgroundToStorage = () => {
                    localStorage.setItem('restaurant-background', backgroundUrl.value);
                };

                // 移除updateBackground方法，因为现在使用Vue响应式绑定

                const generateId = () => {
                    return Date.now() + Math.random().toString(36).substr(2, 9);
                };

                const resetCurrentMenu = () => {
                    currentMenu.value = {
                        name: '',
                        price: '',
                        description: '',
                        imageUrl: ''
                    };
                };

                const openAddModal = () => {
                    isEditing.value = false;
                    currentMenuId.value = null;
                    resetCurrentMenu();
                    showMenuModal.value = true;
                };

                const openEditModal = (menu) => {
                    isEditing.value = true;
                    currentMenuId.value = menu.id;
                    currentMenu.value = { ...menu };
                    showMenuModal.value = true;
                };

                const closeMenuModal = () => {
                    showMenuModal.value = false;
                    resetCurrentMenu();
                };

                const openBackgroundModal = () => {
                    tempBackgroundUrl.value = backgroundUrl.value;
                    showBackgroundModal.value = true;
                };

                const closeBackgroundModal = () => {
                    showBackgroundModal.value = false;
                    tempBackgroundUrl.value = '';
                };

                const saveMenu = () => {
                    if (!currentMenu.value.name.trim() || !currentMenu.value.price) {
                        alert('请填写菜品名称和价格');
                        return;
                    }

                    const menuData = {
                        ...currentMenu.value,
                        price: parseFloat(currentMenu.value.price),
                        name: currentMenu.value.name.trim(),
                        description: currentMenu.value.description.trim()
                    };

                    if (isEditing.value) {
                        const index = menus.value.findIndex(m => m.id === currentMenuId.value);
                        if (index !== -1) {
                            menus.value[index] = { ...menuData, id: currentMenuId.value };
                        }
                    } else {
                        menus.value.unshift({
                            ...menuData,
                            id: generateId(),
                            createdAt: new Date().toISOString()
                        });
                    }

                    saveMenus();
                    closeMenuModal();
                };

                const deleteMenu = (id) => {
                    if (confirm('确定要删除这个菜品吗？')) {
                        menus.value = menus.value.filter(m => m.id !== id);
                        saveMenus();
                    }
                };

                const saveBackground = () => {
                    backgroundUrl.value = tempBackgroundUrl.value.trim();
                    console.log('保存背景:', backgroundUrl.value);
                    saveBackgroundToStorage();
                    closeBackgroundModal();
                };

                const clearBackground = () => {
                    backgroundUrl.value = '';
                    tempBackgroundUrl.value = '';
                    console.log('清除背景');
                    saveBackgroundToStorage();
                    closeBackgroundModal();
                };

                const handleSearch = () => {
                    displayCount.value = 6;
                };

                const loadMore = () => {
                    displayCount.value += 6;
                };

                const previewImage = () => {
                    // 图片预览功能已通过v-if实现
                };

                const handleImageError = (event) => {
                    event.target.style.display = 'none';
                };

                const handlePreviewError = (event) => {
                    event.target.style.display = 'none';
                };

                const handleBackgroundPreviewError = (event) => {
                    event.target.style.display = 'none';
                };

                // 初始化
                onMounted(() => {
                    loadMenus();
                    loadBackground();

                    // 调试信息
                    console.log('页面加载完成，当前背景URL:', backgroundUrl.value);

                    // 添加一些示例数据（如果没有数据的话）
                    if (menus.value.length === 0) {
                        menus.value = [
                            {
                                id: generateId(),
                                name: '红烧肉',
                                price: 28.00,
                                description: '肥而不腻，入口即化的经典红烧肉，选用优质五花肉慢炖而成',
                                imageUrl: 'https://images.unsplash.com/photo-1546833999-b9f581a1996d?w=400',
                                createdAt: new Date().toISOString()
                            },
                            {
                                id: generateId(),
                                name: '宫保鸡丁',
                                price: 22.00,
                                description: '川菜经典，鸡肉嫩滑，花生香脆，酸甜微辣',
                                imageUrl: 'https://images.unsplash.com/photo-1603133872878-684f208fb84b?w=400',
                                createdAt: new Date().toISOString()
                            },
                            {
                                id: generateId(),
                                name: '清蒸鲈鱼',
                                price: 35.00,
                                description: '新鲜鲈鱼，清蒸保持原汁原味，鲜美无比',
                                imageUrl: 'https://images.unsplash.com/photo-1544943910-4c1dc44aab44?w=400',
                                createdAt: new Date().toISOString()
                            }
                        ];
                        saveMenus();
                    }
                });

                return {
                    menus,
                    searchQuery,
                    loading,
                    showMenuModal,
                    showBackgroundModal,
                    isEditing,
                    currentMenu,
                    displayedMenus,
                    hasMore,
                    backgroundUrl,
                    tempBackgroundUrl,
                    backgroundStyle,
                    openAddModal,
                    openEditModal,
                    closeMenuModal,
                    openBackgroundModal,
                    closeBackgroundModal,
                    saveMenu,
                    deleteMenu,
                    saveBackground,
                    clearBackground,
                    handleSearch,
                    loadMore,
                    previewImage,
                    handleImageError,
                    handlePreviewError,
                    handleBackgroundPreviewError
                };
            }
        }).mount('#app');
    </script>
</body>
</html>